package com.day0916.task;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Scanner;

public class FileManager {
    String filePath = "user.txt";

    public String getJsonStr() throws IOException {
        Scanner reader = new Scanner(Paths.get("D:\\IDEA\\lzy_java\\src\\main\\java\\com\\day0916\\task\\user.txt"),"UTF-8");
        String jsonStr = reader.next();
        return jsonStr;
    }

    public String getValue(String jsonStr,String key){
        //int index = jsonStr.indexOf(key);
        jsonStr = jsonStr.replace("{","").replace("\"","").replace("}","");
        String[] str = jsonStr.split(",");
        for (String strA : str){
            //System.out.println(strA);              //    user_name:admin      (strA)
                                                     //    user_password:123456  (strA)
            String[] str2 = strA.split(":");
            for (String strB : str2){
                if (strB.equals(key)){
                    return str2[1];
                }
            }
        }
        return "";
    }

    public void file() throws FileNotFoundException {//逐行读取文件
        BufferedReader reader;
        try {
            reader = new BufferedReader(new FileReader(
                    "src\\com\\day0916\\task\\user.txt"));
            String line = reader.readLine();
            while (line != null) {
                System.out.println(line);
                // read next line
                line = reader.readLine();
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
